package com.bwda.dsrs.systemservice.controller;


import com.bwda.dsrs.base.centerbase.response.ResponseEntity;
import com.bwda.dsrs.systemservice.annotations.IgnoreClientToken;
import com.bwda.dsrs.systemservice.aspect.LogTypeEnum;
import com.bwda.dsrs.systemservice.aspect.Loggable;
import com.bwda.dsrs.systemservice.domain.Constants;
import com.bwda.dsrs.systemservice.domain.condition.datadict.DatadictCondition;
import com.bwda.dsrs.systemservice.domain.po.datadict.DatadictPo;
import com.bwda.dsrs.systemservice.domain.vo.systeminfo.SystemInfoVo;
import com.bwda.dsrs.systemservice.service.DatadictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.bwda.dsrs.systemservice.domain.Constants.HideFunctions.FUNCTION_HIDE;
import static com.bwda.dsrs.systemservice.domain.Constants.HideFunctions.HIDE_DOWNLOAD_CLIENT;

/**
 * 系统信息
 *
 * @author liangjianyong
 * @date 2019/6/4
 */
@RestController
@RequestMapping("/s/systeminfo")
@Api(value = "/s/systeminfo", tags = "系统信息")
public class SystemInfoController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(SystemInfoController.class);

    @Autowired
    private DatadictService datadictService;

    @IgnoreClientToken
    @ApiOperation(value = "查询系统信息", notes = "查询系统信息", httpMethod = "GET")
    @Loggable(descp = "查询系统信息", type = LogTypeEnum.QUERY, include = "")
    @RequestMapping(value = "/queryInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public ResponseEntity<SystemInfoVo> queryInfo() {
        DatadictCondition condition = new DatadictCondition();
        condition.setDataDictTypeCode(Constants.SystemInfo.SYSTEM_INFO_DICT_TYPE);
        condition.setStatus(Constants.StateType.ENABLE);
        List<DatadictPo> poList = datadictService.queryList(condition);
        SystemInfoVo vo = new SystemInfoVo();
        if (poList != null) {
            Map<String, DatadictPo> codeMap = poList.stream().collect(Collectors.toMap(DatadictPo::getDataDictCode, Function.identity(), (k1, k2) -> k2));
            if (codeMap.containsKey(Constants.SystemInfo.SYSTEM_TITLE)) {
                vo.setSystemTitle(codeMap.get(Constants.SystemInfo.SYSTEM_TITLE).getDataDictValue());
            }
            if (codeMap.containsKey(Constants.SystemInfo.PRODUCT_VERSION)) {
                vo.setProductVersion(codeMap.get(Constants.SystemInfo.PRODUCT_VERSION).getDataDictValue());
            }
            if (codeMap.containsKey(Constants.SystemInfo.PUBLISH_DATE)) {
                vo.setPublishDate(codeMap.get(Constants.SystemInfo.PUBLISH_DATE).getDataDictValue());
            }
            if (codeMap.containsKey(Constants.SystemInfo.COPYRIGHT_INFO)) {
                vo.setCopyRightInfo(codeMap.get(Constants.SystemInfo.COPYRIGHT_INFO).getDataDictValue());
            }
            if (codeMap.containsKey(Constants.SystemInfo.SYSTEM_SUBTITLE)) {
                vo.setSystemSubtitle(codeMap.get(Constants.SystemInfo.SYSTEM_SUBTITLE).getDataDictValue());
            }
            if (codeMap.containsKey(Constants.SystemInfo.BUS_WEB)) {
                vo.setBusWeb(codeMap.get(Constants.SystemInfo.BUS_WEB).getDataDictValue());
            }
        }
        // 查询隐藏功能
        DatadictCondition hideCondition = new DatadictCondition();
        hideCondition.setDataDictTypeCode(FUNCTION_HIDE);
        hideCondition.setStatus(Constants.StateType.ENABLE);
        List<DatadictPo> hidePos = datadictService.queryList(hideCondition);
        if (!hidePos.isEmpty()) {
            for (DatadictPo hidePo : hidePos) {
                if (Constants.HideFunctions.HIDE_DOWNLOAD_CLIENT.equals(hidePo.getDataDictCode()) && "1".equals(hidePo.getDataDictValue())) {
                    vo.setHideDownloadClient(true);
                    break;
                }
            }

        }


        return getSuccessResult(vo);
    }

}
